package com.lebron.kotlin.ui.present.impl

import com.lebron.kotlin.BaseApplication
import com.lebron.kotlin.R
import com.lebron.kotlin.ui.present.BasePresentI
import com.lebron.kotlin.ui.view.BaseView
import com.lebron.kotlin.utils.NetworkUtils
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable

/**
 * @Author lebron
 * @Date 18-7-26-下午3:35
 */
abstract class BasePresentImpl<T : BaseView> : BasePresentI<T> {
    override fun attachView(t: T) {
        this.view = t
    }

    override fun detachView() {
        this.view = null
        unDisposable()
    }

    var view: T? = null

    private var compositeDisposable: CompositeDisposable? = null

    /**
     * 添加订阅者
     */
    fun addDisposable(disposable: Disposable) {
        if (compositeDisposable == null) {
            compositeDisposable = CompositeDisposable()
        }
        compositeDisposable?.add(disposable)
    }

    /**
     * 移除订阅者
     */
    private fun unDisposable(){
        compositeDisposable?.clear()
    }

     fun checkNet(){
        if (!NetworkUtils.isConnected()) {
            view?.onError(BaseApplication.instance.applicationContext.getString(R.string.string_network_error))
            return
        }
    }
}